package com.shop.cloud.service.system.impl;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.common.token.PasswordHashUtil;
import com.shop.cloud.dao.hongfu.ShopDao;
import com.shop.cloud.dao.system.SysUserDao;
import com.shop.cloud.dao.system.SysUserRoleDao;
import com.shop.cloud.domain.hongfu.Shop;
import com.shop.cloud.domain.system.SysPosition;
import com.shop.cloud.domain.system.SysUser;
import com.shop.cloud.domain.system.SysUserRole;
import com.shop.cloud.security.EncodeUtil;
import com.shop.cloud.service.system.ISysPositionService;
import com.shop.cloud.service.system.ISysUserService;
import com.shop.cloud.util.Exceptions;
import com.shop.cloud.util.Page;

@Service("sysUserService")
public class SysUserService extends BaseService<SysUser> implements ISysUserService{
    
    @Autowired
	private SysUserDao sysUserDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    
    @Autowired
    private ISysPositionService sysPositionService;
    
    @Autowired
    private ShopDao shopDao;
    
	@Override
	public MybatisDao<SysUser> getEntityDao() {
		return sysUserDao;
	}
	
	@Override
	public int update(SysUser entity) {
		super.update(entity);
		return 1;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int updatePassword(SysUser entity) {
		try {
			entity.setPassword(EncodeUtil.encodeMD5(entity.getPassword()));
		} catch (Exception e) {
			e.printStackTrace();
			throw Exceptions.unchecked(e);
		}
		return sysUserDao.update(entity);
	}
	
	/* 
	* <p>Title: insert</p>
	* <p>Description: </p>
	* @param entity
	* @return
	* @see com.shop.cloud.service.ddd#insert(com.shop.cloud.model.system.SysUser)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean insert(SysUser entity){
		if(!StringUtils.isEmpty(entity.getPassword())){
			try {
				entity.setPassword(entity.getPassword());
			} catch (Exception e) {
				e.printStackTrace();
				throw Exceptions.unchecked(e);
			}
		}
		super.insert(entity);
		return true;
	}

	@Override
	public long findCount(Map<String, Object> params) {
		
		return sysUserDao.findCount(params);
	}

	@Override
	public Page<SysUser> queryUser(SysUser sysuser) {

		
		sysuser.setPageSize((sysuser.getPageSize_()==null?20:sysuser.getPageSize_()));
		long count = sysUserDao.queryUserCount(sysuser);
		Page<SysUser> page = new Page<SysUser>(sysuser.getPageIndex_(), sysuser.getPageSize_());
		page.setData(sysUserDao.queryUser(sysuser));
		page.setTotalCount(count);
		return page;
	}
	
	@Override
	public Map<String, Object> handleUserAuthority(Long userID) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userID);
		//查询职位
		SysUserRole userRole = sysUserRoleDao.findPositionIDByUser(params);
		if(null != userRole){
			SysPosition position = sysPositionService.get(userRole.getPositionId());
			if(null != position){
				result.put("authority", position.getAuthority());
				if(position.getAuthority() == 2){
					result.put("roleId", position.getRoleId());
					
					Map<String, Object> param = new HashMap<String, Object>();
					param.put("roleId", position.getRoleId());
					List<Shop> shopList = shopDao.findThisByRoleId(param);
					if(null != shopList && shopList.size() > 0){
						Long[] shopArray = new Long[shopList.size()];
						for(int i = 0; i < shopList.size(); i ++){
							shopArray[i] = shopList.get(i).getId();
						}
						result.put("shops", shopArray);
					}
				}
			}
		}
		
		return result;
	}

	@Override
	public SysUser findUser(Map<String, Object> findUser) {
		// TODO Auto-generated method stub
		return sysUserDao.findUser(findUser);
	}

}